package cg.project.service;

import cg.project.mapper.UserMapper;
import cg.project.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service // 将这个类的生命周期交给spring的ioc容器来管理
public class UserServiceImpl implements UserService {
    //service层调用mapper层
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String email) {
        //因为此处的业务逻辑比较简单,所以直接调用mapper结束
        User user = userMapper.findByEmail(email);
        //进一步要去确认user的一些信息.
        //TODO... 还需要很多的业务逻辑的处理
        return userMapper.findByEmail(email);
    }

    @Override
    public User SearchUsers(String searchQuery) {

        return userMapper.findByUser(searchQuery);
    }

    @Override
    public User SearchUsersByid(int id) {

        return userMapper.findByUserId(id);
    }

    @Override
    public List<User> getAllUsers() {

        return userMapper.findAll();
    }

    @Override
    public void DeleteUser(int userId) {
        userMapper.deleteById(userId);
    }

    @Override
    public void Updateuser(int id,String username,String email,String pwd){
        userMapper.updateById(id,username,email,pwd);
    }

    @Override
    public PageInfo<User> pageInfo(Integer pageNum, Integer pageSize) {
        //必须先设置分页的参数
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper - 查看所有的
        List<User> users = userMapper.findAll();
        //pageInfo这个对象封装了所有关于分页的信息
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }
}